<?php
defined('ENTER_ID') or die('Hacking attempt');
/**
 * Created by PhpStorm.
 * User: wzl
 * Date: 2017/5/15
 * Time: 18:37
 */
class Autoload
{
    /**
     * 自动加载方法
     * @param string $class
     * @return boolean
     */
    public static function loadClass($class) {
        $path = self::_load_controller($class);
        if($path !== false) {
            require($path);
            return true;
        }
        $path = self::_load_map($class);
        if($path !== false) {
            require($path);
            return true;
        }
        return false;
    }

    /**
     * 加载map配置里的文件
     * @param $class
     * @author: wzl
     * @date: 2017
     * @return bool
     */
    private static function _load_map($class)
    {
        $data = C('_map');
        if(isset($data[$class])) {
            return $data[$class];
        }
        return false;
    }
    /**
     * 加载Controller,兼容映射和Controller
     * @param type $class
     * @return boolean|string
     */
    private static function _load_controller($class) {
        $key = C('_controller');
        if(strpos($class, $key) !== false) {
            $path = CONTROLLER_PATH . $class . EXT;
            if(is_file($path)) {//先判断是否存在此文件
                return $path;
            }
        }
        return false;
    }
}